package hn.info.hockeynews.service;
/**
 * 
 * @author Gerald Hambrosch
 * 
 */
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Environment;
import android.os.IBinder;
import android.widget.Toast;

public class GeoLoggerService extends Service implements LocationListener {
	private String provider = LocationManager.GPS_PROVIDER;
	LocationManager mylm;
	private final String filename = "data.txt";
	String lng;
	String lat;
	private File extFile;
	Location loc;

	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		Toast.makeText(this, "Service created ...", Toast.LENGTH_LONG).show();
		
        mylm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		mylm.requestLocationUpdates(provider, 400, 1, GeoLoggerService.this);
	}
	
	public void writeToFile() throws IOException {
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		Toast.makeText(this, "Service destroyed ...", Toast.LENGTH_LONG).show();
	}

	@Override
	public void onLocationChanged(Location location) {
		lat = String.valueOf(location.getLatitude());
		lng = String.valueOf(location.getLongitude());

		extFile = new File(Environment.getExternalStorageDirectory(), filename);
		if(extFile.exists()) {
			try {
				OutputStreamWriter osWriter = new OutputStreamWriter(
						new FileOutputStream(extFile, true), "UTF-8");
		              	BufferedWriter buWriter = new BufferedWriter(osWriter);
		              	buWriter.write("Long: "+lng+"\nLat: "+lat);
		              	buWriter.newLine();
		              	buWriter.write("-----------------");
		              	buWriter.newLine();
		              	buWriter.close();
			}
			catch (Exception e) {
				Toast.makeText(getBaseContext(), e.getMessage(),
				Toast.LENGTH_SHORT).show();
			}
			return;
		}
		else {
			try {
				extFile.createNewFile();
				try {
					OutputStreamWriter osWriter = new OutputStreamWriter(
							new FileOutputStream(extFile, true), "UTF-8");
			              	BufferedWriter buWriter = new BufferedWriter(osWriter);
			              	buWriter.write("-----*Geodata-Logger*-----");
			              	buWriter.newLine();
			              	buWriter.write("Long: "+lng+"\nLat: "+lat);
			              	buWriter.newLine();
			              	buWriter.write("-----------------");
			              	buWriter.newLine();
			              	buWriter.close();
				}
				catch (Exception e) {
					Toast.makeText(getBaseContext(), e.getMessage(),
					Toast.LENGTH_SHORT).show();
				}
			}
			catch (IOException e) {
				e.printStackTrace();
			}
		}	
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
	}
}
